package edu.nf.supermarket.web.controller.app;

import edu.nf.supermarket.base.BaseController;
import edu.nf.supermarket.entity.Classify;
import edu.nf.supermarket.entity.Commodity;
import edu.nf.supermarket.result.ResultVO;
import edu.nf.supermarket.service.ClassifyService;
import edu.nf.supermarket.service.CommodityService;
import edu.nf.supermarket.service.RecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @author yjj
 * @date 2025-05-26
 */
@RestController
@RequestMapping("/index")
public class IndexController extends BaseController {
    @Autowired
    private CommodityService commodityService;
    @Autowired
    private ClassifyService classifyService;
    @Autowired
    private RecommendService recommendService;

    @GetMapping("/findAll")
    public ResultVO<List<Commodity>> findAllCommodity(@RequestParam("categoryId") Integer classifyId) {
        if (classifyId == 0) {
            List<Commodity> allCommodity = commodityService.findAllCommodity();
            return success(allCommodity);
        } else {
            List<Commodity> commodityByClassify = commodityService.findCommodityByClassify(classifyId);
            return success(commodityByClassify);
        }

    }

    @GetMapping("/findAllClassify")
    public ResultVO<List<Classify>> findAllClassify() {
        List<Classify> allClassify = classifyService.findAllClassify();
        return success(allClassify);
    }

    /**
     * 轮播图推荐商品
     */
    @GetMapping("/findRecommend")
    public ResultVO<List<Commodity>> findCommodityByName(@RequestParam("userId") Integer userId){
        List<Commodity> commodityByName = recommendService.recommend(userId);
        if(commodityByName.isEmpty() || commodityByName.getFirst() == null){
            List<Commodity> recommend = commodityService.findRecommend();
            System.out.println(Arrays.toString(recommend.toArray()));
            return success(recommend);
        }
        System.out.println(Arrays.toString(commodityByName.toArray()));
        return success(commodityByName);
    }
}
